package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.dao.AttrDao;
import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.vo.AttrAttrgroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;



@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao,
        AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    AttrDao attrDao;
    @Autowired
    AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long groupId) {
        //key 不进行处理.
        final IPage<AttrAttrgroupRelationEntity> page1 =
                new Query<AttrAttrgroupRelationEntity>()
                        .getPage(params, "attr_sort",
                                true);

        final QueryWrapper<AttrAttrgroupRelationEntity> wrapper =
                new QueryWrapper<>();
        if (groupId > 0) {
            wrapper.eq("attr_group_id", groupId);
        }
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                page1, wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public AttrAttrgroupRelationVo getVo(AttrAttrgroupRelationEntity entity) {
        AttrAttrgroupRelationVo vo = new AttrAttrgroupRelationVo();
        BeanUtils.copyProperties(entity, vo);
        final AttrEntity attrEntity = attrDao.selectById(entity.getAttrId());
        final AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(entity.getAttrGroupId());
        vo.setAttrName(attrEntity.getAttrName());
        vo.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        return vo;
    }

    @Override
    public void removeBinds(List<AttrAttrgroupRelationEntity> asList) {
        this.baseMapper.removeBinds(asList);
    }
}